const { requestQuery } = require("../database/db"); //按需引入
//分类列表
exports.categoryList = async(req, res) => {
    // 总共多少页
    let page = req.query.page || 1;
    // 一页几条数据
    let size = req.query.size || 2;
    // 跳过多少条数据
    let jump = (page - 1) * size;
    // 总条数
    let countsql = `select count(*) from category where good_id=0 `;
    let count = await requestQuery(countsql);
    let total = count[0]['count(*)'];
    //总页数
    let totalPage = Math.ceil(total / size);
    // 一级分类
    let categorySql = `select * from category where good_id=0 order by cate_id limit ${jump},${size}`;
    let onecategory = await requestQuery(categorySql);
    // 每个一级分类添加空数组
    onecategory.forEach(item => {
        item.children = [];
    });
    //二级分类
    for (var i = 0; i < onecategory.length; i++) {
        let categorytwosql = `select * from category where good_id=${onecategory[i].cate_id}`;
        let twocategory = await requestQuery(categorytwosql);
        twocategory.forEach(item => {
            item.children = [];
        })
        onecategory[i].children = twocategory;
        //三级分类
        for (var j = 0; j < twocategory.length; j++) {
            // console.log(111);
            let categorytheresql = `select * from category where good_id=${twocategory[j].cate_id}`;
            // console.log(categorytheresql);
            let therecategory = await requestQuery(categorytheresql);
            // console.log(therecategory);
            onecategory[i].children[j].children = therecategory;
        }
    }
    res.json({
        page: page,
        size: size,
        total: total,
        totalPage: totalPage,
        data: onecategory,
    })
}